layui.use(['form','layer','layedit','laydate','upload'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    $(function () {
        var id = getUrlParms("id");
        if(id!=null){
            axios.get("/personal_blog/links/"+id).then(function (reponse) {
                var bean = reponse.data;
                $("#beanId").val(bean.id);
                $(".linkName").val(bean.linkName);
                $(".linkUrl").val(bean.linkUrl);
            })
        }
        form.render();
    })


    form.verify({
        linkName : function(val){
            if(val == ''){
                return "链接名称不能为空";
            }
        },
        linkUrl : function(val){
            if(val == ''){
                return "链接地址不能为空";
            }
        }
    })
    form.on("submit(save)",function(data){

        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        var bean = {
            id:$("#beanId").val(),
            linkName:$(".linkName").val(),
            linkUrl:$(".linkUrl").val()
        };
        if($("#beanId").val()){

            axios.put("/personal_blog/links",bean).then(function(response){
                setTimeout(function(){
                    top.layer.close(index);
                    top.layer.msg("友情链接修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            })
            return false;
        }

        axios.post("/personal_blog/links",bean).then(function(response){
            setTimeout(function(){
                top.layer.close(index);
                top.layer.msg("友情链接添加成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            },500);

        })

        return false;
    })

    function getUrlParms(para){
        var search=location.search; //页面URL的查询部分字符串
        var arrPara=new Array(); //参数数组。数组单项为包含参数名和参数值的字符串，如“para=value”
        var arrVal=new Array(); //参数值数组。用于存储查找到的参数值

        if(search!=""){
            var index=0;
            search=search.substr(1); //去除开头的“?”
            arrPara=search.split("&");

            for(i in arrPara){
                var paraPre=para+"="; //参数前缀。即参数名+“=”，如“para=”
                if(arrPara[i].indexOf(paraPre)==0&& paraPre.length<arrPara[i].length){
                    arrVal[index]=decodeURI(arrPara[i].substr(paraPre.length)); //顺带URI解码避免出现乱码
                    index++;
                }
            }
        }

        if(arrVal.length==1){
            return arrVal[0];
        }else if(arrVal.length==0){
            return null;
        }else{
            return arrVal;
        }
    }




})